﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using DAL;
using Model;

namespace BLL
{
    public class BllCollege
    {
        private DalCollege dal = new DalCollege();

        public bool Create(College college, out string strMsg)
        {
            bool blnRet = false;
            //检查CollegeName有没重名
            if (dal.Exists(college.CollegeName, out strMsg))
                strMsg = (strMsg.Length > 0) ? strMsg : "创建失败！已有学院名称： " + college.CollegeName;
            else
                blnRet = dal.Create(college, out strMsg);
            return blnRet;
        }

        public bool Delete(College college, out string strMsg)
        {
            bool blnRet = false;
            //检测Student表有该学院的数据吗？如果有数据，则不能删除，否则可以删除
            DalStudent dalStu = new DalStudent();
            string strWhere = string.Format("WHERE (CollegeId = '{0}')", college.CollegeId.ToString());
            List<Student> lstStu = dalStu.GetAll(strWhere, out strMsg);
            if (strMsg.Length > 0)
            { }
            else if (lstStu.Count > 0)
                strMsg = "删除失败！该学院仍有学生！";
            else
                blnRet = dal.Delete(college, out strMsg);
            return blnRet;
        }

        public College Get(string strCollegeName, out string strMsg)
        {
            return dal.Get(strCollegeName, out strMsg);
        }

        public List<College> GetAll(string strWhere, out string strMsg)
        {
            return dal.GetAll(strWhere, out strMsg);
        }

        public bool Update(College college, out string strMsg)
        {
            bool blnRet = false;
            //检查CollegeName有没重名
            College ent = dal.Get(college.CollegeName, out strMsg);
            if (strMsg.Length > 0)
            { }
            else if (ent.CollegeId != college.CollegeId)
                strMsg = "更新失败！已有学院名称： " + college.CollegeName;
            else
                blnRet = dal.Update(college, out strMsg);
            return blnRet;
        }

        public Guid GetID(string strCollegeName, out string strMsg)
        {
            return dal.GetID(strCollegeName, out strMsg);
        }
    }
}
